package com.jeffse.light.stream.app.listener;

import com.jeffse.light.stream.core.connector.pulsar.PulsarListener;
import com.jeffse.light.stream.core.data.context.SourceContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;

@Slf4j
public class MyPulsarListener implements PulsarListener<String> {
    private SourceContext<String> ctx = null;

    @Override
    public MyPulsarListener setCtx(SourceContext<String> ctx) {
        this.ctx = ctx;
        return this;
    }

    @Override
    public void received(Consumer consumer, Message message) {
        log.info("received: " + new String(message.getData()));
        try {
            synchronized (consumer){
                ctx.collect(new String(message.getData()));
                consumer.acknowledge(message);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
